// 这个组件是添加商品
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品添加</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 步骤条导航 -->
    <el-steps :active="activeName-0" finish-status="success">
      <el-step title="步骤 1"></el-step>
      <el-step title="步骤 2"></el-step>
      <el-step title="步骤 3"></el-step>
      <el-step title="步骤 4"></el-step>
      <el-step title="步骤 5"></el-step>
    </el-steps>
    <!-- 表单 -->
    <el-card class="box-card">
      <el-form label-width="80px">
        <!-- 标签 -->
        <el-tabs
          :before-leave="beforeLeave"
          v-model="activeName"
          :tab-position="'left'"
          @tab-click="handleClick"
          style="margin-top:20px"
        >
          <el-tab-pane label="基本信息" name="0" v-model="goodFrom">
            <el-form-item label="商品名称">
              <el-input v-model="goodFrom.goods_name"></el-input>
            </el-form-item>
            <el-form-item label="商品价格">
              <el-input v-model="goodFrom.goods_price"></el-input>
            </el-form-item>
            <el-form-item label="商品重量">
              <el-input v-model="goodFrom.goods_weight"></el-input>
            </el-form-item>
            <el-form-item label="商品数量">
              <el-input v-model="goodFrom.goods_number"></el-input>
            </el-form-item>
            <el-form-item label="商品分类">
              <el-cascader
                :options="options"
                change-on-select
                :props="optionsProp"
                v-model="goodFrom.goods_cat"
              ></el-cascader>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品参数" name="1">
            <el-checkbox-group
              v-model="item.attr_vals"
              v-for="item in attrValues"
              :key="item.attr_id"
            >
              <el-checkbox
                v-for="(second,index) in item.attr_vals"
                :key="index"
                :label="second"
                border
              ></el-checkbox>
            </el-checkbox-group>
          </el-tab-pane>
          <el-tab-pane label="商品属性" name="2">
            <el-form-item :label="item.attr_name" v-model="staticValues" v-for="item in staticValues" :key="item.attr_id">
              <el-input :value="item.attr_vals"  readonly></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="商品图片" name="3">
            <el-upload
              class="upload-demo"
              :headers="setToken()"
              action="http://localhost:8888/api/private/v1/upload"
              :before-upload="beforeUpload"
              :on-success="handleSuccess"
              :on-preview="handlePreview"
              :on-remove="handleRemove"
              :file-list="fileList"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
            </el-upload>
          </el-tab-pane>
          <el-tab-pane label="商品内容" name="4">
            <quillEditor v-model="goodFrom.goods_introduce"></quillEditor>
          </el-tab-pane>
        </el-tabs>
        <el-button type="success" style="float:right;margin-top:10px" @click="add">添加商品</el-button>
      </el-form>
    </el-card>
  </div>
</template>

<script>
// 富文本框样式的引入
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import { quillEditor } from 'vue-quill-editor'
import { getCatelist } from '@/api/cate-index.js'
import { getAllparamsList } from '@/api/params-index.js'
import { addGoods } from '@/api/goodList-index.js'
export default {
  data () {
    return {
      staticValues: [],
      attrValues: [], // 动态参数数组，它应该是一个对象数组，包含着所有组的参数数据
      fileList: [],
      goodFrom: {
        goods_name: '',
        goods_price: '',
        goods_number: '',
        goods_weight: '',
        goods_cat: '',
        goods_introduce: '',
        pics: [],
        attrs: []
      },
      tabPosition: 'left',
      activeName: '0',
      options: [],
      optionsProp: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      }
    }
  },
  // 富文本框的注册
  components: {
    quillEditor
  },
  methods: {
    // 添加商品
    async add () {
      // 历遍数组
      for (var i = 0; i < this.attrValues.length; i++) {
        let id = this.attrValues[i].attr_id
        console.log(id)
        for (var j = 0; j < this.attrValues[i].attr_vals.length; j++) {
          // 遍历组中的参数数据
          this.goodFrom.attrs.push({
            attr_id: id,
            attr_value: this.attrValues[i].attr_vals[j]
          })
        }
      }
      this.goodFrom.goods_cat = this.goodFrom.goods_cat.join(',')
      let res = await addGoods(this.goodFrom)
      console.log(res)
      // console.log(this.goodFrom)
    },
    // 标签页跳转之前触发的钩子
    beforeLeave (activeName, oldActiveName) {
      // console.log(activeName, oldActiveName)
      if (activeName === '1' || activeName === '2') {
        if (this.goodFrom.goods_cat.length !== 3) {
          this.$message.warning('请先选择分类')
          this.activeName = '0'
          return false
        }
      }
    },
    // 处理标签页的单击，获取参数数据
    async handleClick () {
      // 如果activeName为1，那么应该获取动态数据，如果activeName为2，应该获取静态数据
      if (this.activeName === '1') {
        let res = await getAllparamsList(this.goodFrom.goods_cat[2], 'many')
        // console.log(res)
        this.attrValues = res.data.data
        // 将数组里面的成员进行遍历，并将成员的attr_vals转换为数组
        for (var i = 0; i < this.attrValues.length; i++) {
          this.attrValues[i].attr_vals = this.attrValues[i].attr_vals.split(
            ','
          )
          // console.log(this.attrValues)
        }
      } else {
        if (this.activeName === '2') {
          let res = await getAllparamsList(this.goodFrom.goods_cat[2], 'only')
          this.staticValues = res.data.data
          // console.log(this.staticValues)
        }
      }
    },
    // 设置token
    setToken () {
      // 获取token
      let token = localStorage.getItem('itcast_manager_35_token')
      // 2.返回对象
      return { Authorization: token }
    },
    // 图片上传前钩子
    beforeUpload (file) {
      // console.log(file)
      // 图片的mime类型都是以image/开头，所以我们可以获取到type数据，进行判断
      if (file.type.indexOf('image/') !== 0) {
        this.$message.error('请选择合适类型的文件')
        // 取消当前上传操作，会触发handleRemove
        return false
      }
    },
    // 图片上传成功的钩子
    handleSuccess (response, file, fileList) {
      // console.log(response)
      // console.log(file)
      // console.log(fileList)
      // // 获取文件上传成功之后，服务器端的文件存储路径
      if (response.meta.status === 200) {
        // response包含了我们需要的文件上传成功之后服务器端的存储路径
        this.goodFrom.pics.push({ pic: response.data.tmp_path })
      }
    },
    // 移除图片--移除数据的存储
    handleRemove (file, fileList) {
      // console.log(file, fileList)
      if (!file.response) {
        return
      }
      // file就是当前被你移除的图片对象
      // 我们需要获取这个被移除的图片对象，将对应名称的数据从goods_form中删除
      var temp = file.response.data.tmp_path
      for (var i = 0; i < this.goodFrom.pics.length; i++) {
        if (this.goodFrom.pics[i].pics === temp) {
          this.goodFrom.pics.splic(i, 1)
          break
        }
      }
    },
    handlePreview (file) {
      // console.log(file)
    }
  },
  mounted () {
    getCatelist(3).then(res => {
      // console.log(res)
      this.options = res.data.data
    })
  }
}
</script>

<style lang="less">
</style>
